package com.koron.css2.newProjectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.query.PjInfoQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.vo.PjInfoListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 工程款调整列表查询
 * @author xusheng
 *
 */
public class PjAdjustList implements ServerInterface {
	
	@Override
	@SuppressWarnings("rawtypes")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//将入参转换为QueryBean
			PjInfoQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), PjInfoQueryBean.class);
			if(StringUtils.isBlank(queryBean.getBeginDate())||StringUtils.isBlank(queryBean.getEndDate())) {
				return MessageBean.create(Constant.NOT_NULL, "报装日期不能为空", null);
			}
			//获取Mapper实例
			PjInfoMapper mapper = factory.getMapper(PjInfoMapper.class);
			//设置分页参数
			PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
			//执行SQL
			List<PjInfoListVO> list = mapper.selectAdjust(queryBean);
			//获取来源渠道数据字典
			Map<String, String> PIS = Tools.mapDicByCode(factory, "PIS");
			//获取工程类型数据字典
			Map<String, String> PPT = Tools.mapDicByCode(factory, "PPT");
			for(PjInfoListVO bean:list) {
				//转换数据字典
				bean.setSourceFlag(PIS.get(bean.getSourceFlag()));
				bean.setPjType(PPT.get(bean.getPjType()));
				//转换状态
				if("suspend".equals(bean.getProcessState())) {
					bean.setProcessState("已暂停");
				}else {
					bean.setProcessState(Tools.transProcessStatus(bean.getProcessState()));
				}
				//超期状态
				if("1".equals(bean.getTimeOutFlag())) {
					bean.setTimeOutFlagName("已超期");
				}else {
					bean.setTimeOutFlagName("未超期");
				}
			}
			//输出分页结果
			MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
			info.setData(new PageInfo<>(list));
			return info;
		}catch(Exception e){
			factory.close(false);
			logger.error("工程款调整明细查询失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
